package com.baizhiedu.rsm.service.impl;

import com.baizhiedu.rsm.dao.DeleteStudentDao;
import com.baizhiedu.rsm.dao.StudentDao;

import com.baizhiedu.rsm.entity.Student;
import com.baizhiedu.rsm.entity.Subject;
import com.baizhiedu.rsm.service.StudentServcie;
import com.baizhiedu.rsm.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lby
 */
@Service
public class StudentServcieImpl implements StudentServcie {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private DeleteStudentDao deleteStudentDao;
    @Autowired
    private SubjectService subjectService;

    @Override
    public List<Student> selectStudentByIds(Integer[] id) {
        return studentDao.findStudentByIds(id);
    }

    @Override
    public int insert(Student record) {
//        if (record != null){
////            添加至学生表
//            int insert = studentDao.insert(record);
//            String name = record.getName();
////            获取学生id 此时不应该使用名字作为查询的标准  名字不是唯一的 手机号
//            record = studentDao.selectStudentByName(name);
//            //添加至用户登录表
////            userLoginDao.insert(new UserLogin(record.getName(),"123456",record.getClassid(),record.getId()));
//        }
        return 1;
    }

    @Override
    public List<Student> showAllStudentByClassId(Integer classId) {
        return studentDao.selectAllStudentByClassId(classId);
    }

    @Override
    public Map updateOne(Student student) {
        Map map = new HashMap(16);
        try{
            studentDao.updateByPrimaryKey(student);
            map.put("update",true);
        }catch (Exception e){
            map.put("update",false);
        }
        return map;
    }

    @Override
    public int deleteSelect(List<Student> students) {
        for (Student s:students) {
            Integer id = s.getId();
            //删除之前 把数据查询出来放入DeleteStudent表
//            deleteStudentDao.insert(studentDao.selectByPrimaryKey(id));
            //在原表中删除
            studentDao.deleteByPrimaryKey(id);
        }
        return 0;
    }

    @Override
    public List<Subject> addSchedule(Integer studentId, Integer lastId) {
        //得到学生上一次保存的sid
        Student student = studentDao.selectByPrimaryKey(studentId);
        Integer frontId = Integer.parseInt(student.getSchedule());

//        进度检查 目前的标准是每天必须看至少两个视频
        if(lastId>frontId && lastId-frontId<2){
//            设置为重点关注学生
            student.setFocus("是");
        }
//        进度更新
        if(lastId == null){
            System.out.println("进度参数为空，不执行添加操作~~~");
        }else {
            student.setSchedule(lastId.toString());
            studentDao.updateByPrimaryKey(student);
        }
//        题目获取 如果lastid比之前的小 如果没有 调用题目获取方法
        return subjectService.showSubjectBySid(frontId,lastId);
    }

    @Override
    public Student getStudentById(Integer studentid) {
        return studentDao.selectByPrimaryKey(studentid);
    }
}
